﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace _7zDel
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string arg in args)
            {
                String packThis = arg;

                if (File.Exists(packThis))
                { // File
                    FileInfo file = new FileInfo(packThis);
                    String archive = Path.Combine(file.Directory.FullName, file.Name);

                    PackIt(String.Format(@"u ""{0}"" ""{1}""", archive, file.FullName + ".[7zDel].7z"));

                    if (File.Exists(archive))
                    {
                        Unprotect(file);
                        file.Delete();
                    }
                }
                else if (Directory.Exists(packThis))
                { // Directory
                    DirectoryInfo directory = new DirectoryInfo(packThis);
                    String archive = Path.Combine(directory.Parent.FullName, directory.Name + ".[7zDel].7z");

                    PackIt(String.Format(@"u ""{0}"" ""{1}""", archive, directory.FullName));

                    if (File.Exists(archive))
                    {
                        UnprotectDirectory(directory);
                        directory.Delete(true);
                    }
                }
            }

            

#if DEBUG
            // C:\Program Files\7-Zip>7z u "C:\!Personal\Coding (VS)\VS 2008\7z.del\7z.del\bin\Debug\test2" "C:\Users\Dennis\Documents\Visual Studio 2008\Projects\VSMacros80\Samples\Samples.vsmacros"
            Console.ReadKey();
#endif
        }

        //Unprotect and shit
        private static void Unprotect(FileSystemInfo fsi)
        {
            // UnHide
            if ((FileAttributes.Hidden) == (fsi.Attributes & FileAttributes.Hidden)) // bitwise comparison
            {
                fsi.Attributes -= FileAttributes.Hidden;
            }

            // UnProtect
            if ((FileAttributes.ReadOnly) == (fsi.Attributes & FileAttributes.ReadOnly)) // bitwise comparison
            {
                fsi.Attributes -= FileAttributes.ReadOnly;
            }
        }
        private static void UnprotectDirectory(DirectoryInfo directory)
        {
            Unprotect(directory);

            foreach (FileInfo file in directory.GetFiles())
                Unprotect(file);

            foreach (DirectoryInfo subDirectory in directory.GetDirectories())
                UnprotectDirectory(subDirectory);
        }

        private static void PackIt(String arguments)
        {
            ProcessStartInfo psi = new ProcessStartInfo("7z.exe", arguments);
            psi.CreateNoWindow = false;
            psi.UseShellExecute = false;

            Process proc = new Process();
            proc.StartInfo = psi;
            proc.Start();
            proc.WaitForExit();
            proc.Dispose();
        }
    }
}
